package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "订单模块")
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    @GetMapping("/conditionSearch")
    @ApiOperation("条件查询订单")
    public Result<PageResult> conditionOrder(OrdersPageQueryDTO ordersPageQueryDTO) {

        log.info("ordersPageQueryDTO={}", ordersPageQueryDTO);
        PageResult pageResult = ordersService.getOrdersByCondition(ordersPageQueryDTO);
        return Result.success(pageResult);

    }

    @GetMapping("/details/{orderId}")
    @ApiOperation("查看订单详情")
    public Result<OrderVO> showOrderDetail(@PathVariable Long orderId) {

        log.info("orderId={}", orderId);
        OrderVO orderVO = ordersService.getByOrderDetail(orderId);
        return Result.success(orderVO);

    }

    @PutMapping("/confirm")
    @ApiOperation("接收订单")
    public Result<String> confirmOrder(@RequestBody OrdersDTO ordersDTO) {

        log.info("confirmOrder={}", ordersDTO);
        ordersService.accept(ordersDTO);
        return Result.success();

    }

    @GetMapping("/statistics")
    @ApiOperation("统计订单数量")
    public Result<OrderStatisticsVO> statisticsOrders(){

        OrderStatisticsVO orderStatisticsVO = ordersService.statistics();
        return Result.success(orderStatisticsVO);

    }

    @PutMapping("cancel")
    @ApiOperation("取消订单")
    public Result<String> cancelOrder(@RequestBody OrdersCancelDTO ordersCancelDTO){

        ordersService.cancelByAdmin(ordersCancelDTO);
        return Result.success();

    }

    @PutMapping("/rejection")
    @ApiOperation("拒单")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        ordersService.rejection(ordersRejectionDTO);
        return Result.success();
    }
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result delivery(@PathVariable("id") Long id) {
        ordersService.delivery(id);
        return Result.success();
    }

    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result complete(@PathVariable("id") Long id) {
        ordersService.complete(id);
        return Result.success();
    }




}
